package cn.wyf.androidcode.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonElement;

import net.wyf.library.observe.ApiCallback;
import net.wyf.library.utils.glide.GlideHelper;
import net.wyf.library.utils.log.XLogger;
import net.wyf.library.utils.log.XToast;
import net.wyf.library.utils.log.Xsp;
import net.wyf.library.widget.NoScrollGridView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wyf.androidcode.R;
import cn.wyf.androidcode.adapter.DialogGridAdapter;
import cn.wyf.androidcode.application.BaseActivity;
import cn.wyf.androidcode.application.CodeContast;
import cn.wyf.androidcode.bean.DataCollect;
import cn.wyf.androidcode.bean.FirstVideo;
import cn.wyf.androidcode.bean.Info;
import cn.wyf.androidcode.bean.User;
import cn.wyf.androidcode.di.modules.RequestHelper;
import io.reactivex.disposables.Disposable;

public class MainActivity extends BaseActivity {

    public String picUrl = "http://120.202.127.36:21580/image/headPortrait/100621_1498200320723.jpg";

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageView = (ImageView) findViewById(R.id.show);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
            }
        });
        init();
        showDialog();
    }

    String[] strs = {"不傲慢与", "不满意是", "你哈死啊", "你作死啊"};
    List<Boolean> is = new ArrayList<>();

    private void showDialog() {
        List<String> datas = new ArrayList<>();
        for (String s : strs
                ) {
            datas.add(s);
        }

        List<Boolean> isChecked = new ArrayList<>();
        isChecked.add(true);
        isChecked.add(false);
        isChecked.add(false);
        isChecked.add(false);
        is.addAll(isChecked);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  //先得到构造器
        Dialog dialog = builder.create();
        dialog.show();
        Window window = dialog.getWindow();
        View mydialog = getLayoutInflater().inflate(R.layout.mydialog, null, false);
        window.setContentView(mydialog);

        NoScrollGridView content = (NoScrollGridView) mydialog.findViewById(R.id.content);
        DialogGridAdapter adapter = new DialogGridAdapter(MainActivity.this, datas, is);
        content.setAdapter(adapter);
        content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                XLogger.v("========" + position);
                is.clear();
                for (int i = 0; i < datas.size(); i++) {
                    if (i == position) {
                        isChecked.add(i, true);
                    } else {
                        isChecked.add(i, false);
                    }
                }
                is.addAll(isChecked);
                adapter.notifyDataSetChanged();
            }
        });
        TextView updateTextView = (TextView) mydialog.findViewById(R.id.center);
        updateTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView cancleTextView = (TextView) mydialog.findViewById(R.id.cancel);
        cancleTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void setLayout() {
        setContentView(R.layout.activity_main);
    }

    private void init() {
        GlideHelper.loadAvatar(MainActivity.this, picUrl, imageView);

        Xsp.getInstance().put("123", "你好啊");
        String data = Xsp.getInstance().getString("123", "");
        XLogger.d(data);
        XToast.showShort(data);

        Map<String, String> params = new HashMap<>();
        params.put("phoneNo", "17710880054");
        params.put("passwd", "e10adc3949ba59abbe56e057f20f883e");

        login(params);

//        datacollect();

//        getFirstVideo();

//        getInfo();

    }

    private void datacollect() {
        addSubscription(RequestHelper.getInstance().doGetData(CodeContast.DATA_COLLECT), new ApiCallback<DataCollect>() {
            @Override
            public void onHandleSuccess(DataCollect response) {
                XLogger.v(response.toString());
                String blacklist = response.getBlacklist().toString();
                List<String> list = new ArrayList<>();
                try {
                    JSONArray array = new JSONArray(blacklist);
                    for (int i = 0; i < array.length(); i++) {
                        list.add(array.getString(i));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                XLogger.d(list.toString());
                for (String s : list
                        ) {

                    XLogger.d("onStartCommand: --------------" + s);
                }
            }

            @Override
            public void onHandleFailure(String msg) {
                XLogger.e("msg");
            }

            @Override
            public void onHandleFinish() {
                XLogger.v("onHandleFinish");
            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
            }
        });
    }

    private void login(Map<String, String> params) {
        addSubscription(RequestHelper.getInstance().doLogin(params), new ApiCallback<User>() {
            @Override
            public void onSubscribe(Disposable d) {
//                compositeDisposable.add(d);
            }

            @Override
            public void onHandleSuccess(User response) {
                XLogger.v(response.toString());
            }

            @Override
            public void onHandleFailure(String msg) {
                XLogger.e(msg);
            }

            @Override
            public void onHandleFinish() {
                XLogger.v("onHandleFinish");
            }

        });
    }

    private void getFirstVideo() {
        addSubscription(RequestHelper.getInstance().doFirstVideo(CodeContast.FIRST_VIDEO), new ApiCallback<List<FirstVideo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
            }

            @Override
            public void onHandleSuccess(List<FirstVideo> response) {
                XLogger.v(response.toString());
            }

            @Override
            public void onHandleFailure(String msg) {
                XLogger.e(msg);
            }

            @Override
            public void onHandleFinish() {
                XLogger.v("onHandleFinish");
            }

        });
    }

    private void getInfo() {
        addSubscription(RequestHelper.getInstance().doInfo(), new ApiCallback<List<Info>>() {
            @Override
            public void onHandleSuccess(List<Info> response) {
                XLogger.v(response.toString());
            }

            @Override
            public void onHandleFailure(String msg) {
                XLogger.e(msg);
            }

            @Override
            public void onHandleFinish() {

            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
            }
        });
    }
}
